package com.iamback.dy.examine.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.iamback.dy.entity.CommentExuser;
import com.iamback.dy.entity.CommentExuserView;
import com.iamback.dy.entity.DyContentComment;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.service.ICommentExuserService;
import com.iamback.dy.examine.service.IDyContentCommentService;
import com.iamback.dy.mapper.CommentExuserMapper;

@Service
public class CommentExuserServiceImpl  implements ICommentExuserService {

	@Autowired
	private CommentExuserMapper  commentExuserDao;
	@Autowired
	private IDyContentCommentService   contentCommentService;
 
	@Override
	public CommentExuser findCommentExuserById(Long id){
		return commentExuserDao.findCommentExuserById(id);
	}
	public void updateCommentExuser(CommentExuser commentExuser){
		  commentExuserDao.updateCommentExuser(commentExuser);
	} 
	
	@Override
	public void saveAll(List<CommentExuser>  list){
		commentExuserDao.insertAll(list);
	}
	
	@Async("distributionAsync")
	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED)
	public synchronized void  distributionBy(Integer userId,Integer size){ 
		if(size!=null&&size <20){ 
			Map<String, Object> findbuilder = new HashMap<String, Object>();
			findbuilder.put("exResult", "0");
			findbuilder.put("exuserIdIsNull", "11");
			findbuilder.put("numPerPage", 100-size);
			List<CommentExuser> pageContentExusers=commentExuserDao.findCommentExuserBy(findbuilder);
			for(CommentExuser  commentExuser:pageContentExusers){
				CommentExuser optional =commentExuserDao.findCommentExuserById(commentExuser.getId());
				if(optional!=null&&optional.getExResult().equals("0")){
					CommentExuser commentExuserUp = new CommentExuser();
					commentExuserUp.setId(optional.getId());
					commentExuserUp.setExResult("0");
					commentExuserUp.setAllotTime(new Date());
					commentExuserUp.setExuserId(userId);
					commentExuserDao.updateCommentExuser(commentExuserUp);
				}
			}
		}
	}
	  
	@Override
	public Page<CommentExuserView> findCommentExuserPage(Integer userId ,Integer pageNumber,Integer pageSize){
		int start = pageNumber ==0?0:(pageNumber-1)*pageSize;
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("exuserId", userId);
		map.put("pageNum", start);
		map.put("numPerPage", pageSize);
		Page<CommentExuserView> page =new  Page<CommentExuserView>();
		Integer count =   commentExuserDao.findCommentExuserPageCount(map);
		if(count != null && count >0){
			page.setContent(commentExuserDao.findCommentExuserPage(map));
			page.setTotalElements(count);
		}
		else{
			page.setTotalElements(0);
		}
		
		return page;
	}
	
	
	
	@Override
	public void updateCommentExuserBlocked(String publisherDyId){ 
		Map<String, Object> findbuilder = new HashMap<String, Object>();
		findbuilder.put("commentDyId", publisherDyId);
		findbuilder.put("exResult", "0");
		List<CommentExuser> commentExusers=commentExuserDao.findCommentExuserBy(findbuilder);
		for(CommentExuser commentExuser:commentExusers){
			commentExuser.setExResult("3"); 
			commentExuser.setExTime(new Date()); 
			commentExuser.setExDesc("禁言操作");
			commentExuser.setExuserId(null);
			commentExuserDao.updateBlockedComment(commentExuser);
			
			DyContentComment contentComment =new DyContentComment();
			contentComment.setCommentId(commentExuser.getCommentId());
			contentComment.setCommentType(3L);
			contentCommentService.updateContentComment(contentComment);
		}
	}

}
